
import { FormationManager } from "GameLoop/Execution/FormationManager/FormationManager";
import { FormationPattern } from "GameLoop/Execution/FormationManager/FormationPattern";
import { ATeamEntity } from "..";
import { ATeamCombat } from "./TeamCombat";
import { Team2Pattern } from "GameLoop/Execution/FormationManager/Pattern/team2pattern";
import { PositionHelper } from "utils/position/PositionHelper";
import { TeamMoveUtils } from "utils/team/TeamMoveUtils";
import { Math2D } from "algorithm/Math2d";
import { CreepAbilityHelper } from "utils/creep/CreepAbilityHelper";
/**
 * 二人小队
 * 
 */
export class Team2 extends ATeamCombat
{
    
 
    protected _Formation!: FormationManager<Creep>   ;
    
    public initAttributes(): void 
    {
        super.initAttributes();
        this._Formation = new FormationManager(this,new Team2Pattern<Creep>());
        for(const e of this.getAttacker())
        {
            this._Formation.addCharactor(e);
        }
        for(const e of this.getHealer())
        {
            this._Formation.addCharactor(e);
        }
    }
        /**
     * 投递孵化爬的任务
     */
    public postSpawn(priority:number): void 
    {
        const spawnentity = this.getEntity("SpawnEntity");
        if(this.getNodeMemory().teamState || spawnentity.getTasksByOwner(this.GUID()))
        {
            return 
        }
        const task = this.postSpawnImpl({} as any,priority);
        if(task)
        {
            task.data.teamid = this.GUID();
        }
    }
    public center()   
    {
       return this.getAnchor()
    }
    public moveByDirection(dir: DirectionConstant): boolean 
    {
        return this.lineMove(dir);
    }
     /**
     * 原地不动
     * @param targetVec 
     */
    public standard(targetVec:IFVector2):boolean
    {
        const team = this;

        const hostfile =TeamMoveUtils.getHostileField(team);
        if(Math.abs(targetVec.x)>1 && Math.abs(targetVec.y)>1&&Math.abs(hostfile.x)>1 && Math.abs(hostfile.y)>1 )
        {
            return false;
        }
        const team_dir:IFVector2=team.getDirection();
        const angle = Math2D.angle(team_dir,targetVec);
        if(team.isInSameroom() && team.creeps.length==2 )
        {
            // 弧度
            
            if(angle>Math.PI/2)
            {
                //
                team.move([ team.creeps[0].pos.getDirectionTo( team.creeps[1]), team.creeps[1].pos.getDirectionTo( team.creeps[0])]);
                return true;
            
            }
            else 
            {
                if(angle!=0)
                {
                    const enemy = _.max( team.aroundHostileCreeps.filter(e=>e.pos.distance(team.getHealer()[0].pos)==1 && e.body.some(e=>e.type==ATTACK)),c=>CreepAbilityHelper.getCreepAttackAbility(c));
                    if(enemy)
                    {
                        const dir =  enemy.pos.getDirectionTo(team.getAttacker()[0] );
                        const p2 = team.getAttacker()[0].pos.directionToPos(dir);
                        if(p2)
                        {
                            const dir2 = team.getHealer()[0].pos.getDirectionTo(p2);
                            team.getHealer()[0].move(dir2);
                        }
                        
                    }
                }
                
            }
        }
        
        
        return false;
    }
    public getAttackableCreeps(): Creep[] {
       
        return this.aroundHostileCreeps.filter(e=>e.pos.distance(this.getAnchor())<=1)
    }
    
    public isInCombatRange(pos: IRoomPosition, range: number=1): boolean 
    {
        const lpos = this.getAttacker()[0]?.pos;
        if(lpos)
        {
            // this.log(`info`,'距离判断',JSON.stringify(pos),"<->",JSON.stringify(lpos),`=${PositionHelper.Distance(lpos,pos)}`,`range:${range}`)
            return PositionHelper.Distance(lpos,pos)<=range
        }
        return false;
    }
    /**
     * 是否以及完成集合
     * @returns 
     */
    public isAssembleOver(): boolean {
        const creeps = this.creeps;
        for(let i=0;i<creeps.length;i++)
        {
            let nextidx= (i+1)%creeps.length;
            if(creeps[i].pos.distance(creeps[nextidx].pos)>1)
            {
                return false;
            }
        }
        return true
    }
 
}